%include "../pm.inc"

org 0100h
	jmp LABEL_BEGIN

[SECTION .gdt]
; GDT begin
LABEL_GDT: Descriptor 0, 0, 0;	// null dscr
LABEL_DESC_CODE32: Descriptor 0, SegCode32Len - 1, DA_C + DA_32;	// 
LABEL_DESC_VIDEO: Descriptor 0B8000h, 0FFFFh, DA_DRW; // 
; end of GDT

GdtLen equ $ - LABEL_GDT;
GdtPtr dw GdtLen - 1;
	   dd 0;	// base of gdt.

SelectorCode32 equ LABEL_DESC_CODE32 - LABEL_GDT;
SelectorVideo  equ LABEL_DESC_VIDEO - LABEL_GDT;
; end of [SECTION .gdt]

[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
	mov ax, cs;
	mov ds, ax;
	mov es, ax;
	mov ss, ax;
	mov sp, 0100h;

; init 32-bit code section descriptor
	xor eax, eax;
	mov ax, cs;
	shl eax, 4;
	add eax, LABEL_SEG_CODE32;
	mov word [LABEL_DESC_CODE32 + 2], ax;
	shr eax, 16;
	mov byte [LABEL_DESC_CODE32 + 4], al;
	mov byte [LABEL_DESC_CODE32 + 7], ah;

; prepare for download GDTR.
	xor eax, eax;
	mov ax, ds;
	shl eax, 4;
	add eax, LABEL_GDT;		eax <- gdt base.
	mov dword[GdtPtr + 2], eax;

	; download GDTR
	lgdt [GdtPtr]
	; close all int.
	cli;

	; open A20 addr.
	in al, 92h;
	or al, 00000010b
	out 92h, al

; prepare for switch to protect mode.
	mov eax, cr0;
	or eax, 1;
	mov cr0, eax;

; enter protect mode
	jmp dword SelectorCode32:0;		// excute this line: cs = SelectorCode32

; end of [SECTION .s16]

[SECTION .s32]
[BITS 32]
LABEL_SEG_CODE32:
	mov ax, SelectorVideo
	mov gs, ax;

	mov edi, (80 * 11 + 79) * 2;
	mov ah, 0Ch;
	mov al, 'P'
	mov [gs:edi], ax;

	jmp $;

	SegCode32Len equ $ - LABEL_SEG_CODE32

; end of [SECTION .s32]


